<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Help;
use app\common\model\Version;
use fast\Http;
use think\Cache;
use think\Config;
use think\Db;
use think\Response;

/**
 * Ajax异步请求接口
 * @internal
 */
class Ajax extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 上传文件
     */
    public function upload()
    {
        return action('api/common/upload');
    }

    /**
     * 上架/上线 隐藏开关
     */
    public function isopen()
    {
        $open = 0;
        $this->success('ok',compact('open'));
    }

    /**
     * 版本更新
     */
    public function version()
    {
        if ($version = $this->request->request('version')) {
            $type = $this->request->request('type');
            $core = $this->request->request('core','Android');
            $versiondata = Version::check($version,$type,$core);
            $content = [
                'versiondata' => $versiondata,
            ];
            $this->success('', $content);
        } else {
            $this->error(__('Invalid parameters'));
        }
    }

    /**
     * 启动页
     */
    public function startup()
    {
        $image = Config::get('site.startup_image');
        $image = $image!=''?cdnurls($image):'';
        $this->success('ok',compact('image'));
    }

    /**
     * 富文本
     */
    public function text()
    {
        $title = input('title','');
        $content = Config::get('site.'.$title);
        $this->success('ok',compact('content'));
    }

    /**
     * 城市列表
     */
    public function city(){
        $keyword = input('keyword');
        $list = Area::where('level',2)
            ->whereLike('name','%'.$keyword.'%')
            ->field("id,name,first")
            ->order("first asc")
            ->select();
        $new_list=[];
        foreach ($list as $value){
            $new_list[strtoupper($value['first'])]['first']=strtoupper($value['first']);
//            $value['area']=Area::where('level',3)
//                ->where('pid',$value['id'])
//                ->field("id,name")
//                ->order("id asc")
//                ->select();
            $new_list[strtoupper($value['first'])]['list'][]=$value;
        }
        $this->success('', array_values($new_list));
    }

    /**
     * 常见问题
     */
    public function help()
    {
        $keyword = trim(input('keyword'));
        $where =[];
        if($keyword!=''){
            $where['title'] = ['like','%'.$keyword.'%'];
        }
        $list =Help::where($where)->field("id,title,content,updatetime")->order("sort asc")->paginate();
        $this->success('ok',$list);
    }




}
